package com.example.sachon;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.example.sachon.DetailContentListAdapter.ViewHolder;
import com.example.sachon.vo.BoardItem;
import com.example.sachon.vo.ChonInfo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ChonBoardNewListAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private int viewResourceId;
	private ArrayList<BoardItem> data;
	// references to our images

	public ChonBoardNewListAdapter(ArrayList<BoardItem> data, int viewResourceId,  Context  context){
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.viewResourceId = viewResourceId;
		this.data = data;
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.activity_board_news_context, null);
			
			viewHolder. _news_title = (TextView) convertView
					.findViewById(R.id.news_title);
			viewHolder._news_user_name= (TextView) convertView
					.findViewById(R.id.news_user_name);
			
			viewHolder. _news_content= (TextView) convertView
					.findViewById(R.id.news_content);
			
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.news_img);
			
			convertView.setTag(viewHolder);

			System.out.println("4--------------->");
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		viewHolder._news_title.setText(data.get(position).getTitle());
		viewHolder._news_user_name.setText(data.get(position).getUser_name());
		viewHolder._news_content.setText(data.get(position).getContent());	
		
		if( data.get(position).getPic_name().isEmpty() == false){ 
			try {
				String urlString = Const.SERVER_IMAGE_URL+data.get(position).getPic_name();
				System.out.println("urlString:["+urlString+"]");
				ImageDownloader.download(urlString, viewHolder.imageView);
				
			} catch (Exception e) {
				e.printStackTrace();
			}		
		}
		return convertView;
	}
	public final class ViewHolder {
		private TextView _news_title;
		private TextView _news_user_name;
		private TextView _news_content;
		private ImageView imageView;
	}

}
